package com.example.qxfw.qxxx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.Satellitenephoram;
import com.example.qxfw.mongodb.qx.dto.QxYtFileDto;
import com.example.qxfw.mongodb.qx.entity.QxYtFile;
import com.example.qxfw.mongodb.qx.service.QxYtFileService;
import com.example.qxfw.qxxx.service.SatellitenephoramService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.qxfw.common.util.DateUtil.convertDateFormat;
import static com.example.qxfw.common.util.DateUtil.convertDateTimeToDateString;
import static com.example.qxfw.common.util.StringUtil.extractAlphanumeric;

@Slf4j
@Api(tags = "气象信息-云图")
@RestController
@CrossOrigin
@RequestMapping("/NephogramController")
@Log4j2
public class NephogramController {

    @Autowired
    private SatellitenephoramService satellitenephoramService;

    @Autowired
    private QxYtFileService qxYtFileService;

    @ApiOperation("气象信息-云图-获取云图数据")
    @GetMapping("/getSatellitenephoram")
    public SysResult getSatellitenephoram(String ytType, String timeStart, String timeEnd) {
        if (timeStart.contains("中国标准时间") ){
            timeStart = convertDateTimeToDateString(timeStart);
        }
        if (timeEnd.contains("中国标准时间")){
            timeEnd = convertDateTimeToDateString(timeEnd);
        }
        List<QxYtFileDto> ytFilePngDtoList = new ArrayList<>();
        List<Satellitenephoram> list = satellitenephoramService.getList(ytType, timeStart, timeEnd);
        List<QxYtFile> qxYtPngFileList = new ArrayList<>();
        list.forEach(dop -> {
            String yttype = dop.getYttype();
            String timechar = dop.getTimechar();
            String datechar = dop.getDatechar().toString();
            String dateTime = convertDateFormat(datechar);
            assert dateTime != null;
            String[] parts = dateTime.split("-");
            String dateyear = parts[0];
            List<QxYtFile> selectIdDateCharTimeChar = qxYtFileService.selectByDateCharTimeChatYtType(yttype,dateTime, timechar);

            qxYtPngFileList.addAll(selectIdDateCharTimeChar);

        });

        List<QxYtFile> qxYtPngFiles = qxYtPngFileList.stream().distinct().collect(Collectors.toList());
        qxYtPngFiles.forEach(q -> {
            String q_id = q.get_id().toString();
            String datechar = q.getDatechar();
            String timechar = q.getTimechar().toString();
            String hours = timechar.substring(0, 2);
            String minutes = timechar.substring(2);
            QxYtFileDto ytFilePngDto = new QxYtFileDto();
            ytFilePngDto.setId(q_id);
            ytFilePngDto.setName(datechar + " " + hours + ":" + minutes + ":00");
            ytFilePngDtoList.add(ytFilePngDto);
        });
        return SysResult.success(ytFilePngDtoList);
    }


    @ApiOperation("气象信息-云图-获取云图名称列表")
    @GetMapping("/getYtFile")
    public SysResult getYtFile(String dateChar, String timeChar, String yttype) {
        System.err.println(dateChar);
        System.err.println(timeChar);
        System.err.println(yttype);
        List<QxYtFileDto> ytFileDtoList = new ArrayList<>();
        dateChar = convertDateTimeToDateString(dateChar);
        System.err.println(dateChar);
        List<QxYtFile> qxYtFiles = qxYtFileService.selectByDateCharTimeChatYtType(yttype, dateChar, timeChar);
        final String finalDateChar = dateChar;
        System.err.println(qxYtFiles);
        qxYtFiles.forEach(yt -> {
            Object id = yt.get_id();
            Object timeChar2 = yt.getTimechar();
            QxYtFileDto qxYtFileDto = new QxYtFileDto();
            qxYtFileDto.setId(id.toString());
            assert finalDateChar != null;
            String replace = finalDateChar.replace("-", "");
            System.err.println(replace);
            qxYtFileDto.setName(replace + "_" + timeChar2 + "_" + extractAlphanumeric(yttype) + ".AWX");
            ytFileDtoList.add(qxYtFileDto);
        });

        return SysResult.success(ytFileDtoList);
    }

    @ApiOperation("气象信息-云图-根据id获取云图")
    @GetMapping("/getYtById")
    public ResponseEntity<byte[]> getYtById(String id, String name) {
        QxYtFile qxYtFile = qxYtFileService.selectById(id);
        byte[] data = qxYtFile.getData();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 设置为图片的MIME类型
        headers.setContentDisposition(ContentDisposition.builder("attachment").filename(name + ".jpg").build());
        return new ResponseEntity<>(data, headers, HttpStatus.OK);
    }


}
